package com.itheima.springboottilasmanager.survice.deptsurvice;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.itheima.springboottilasmanager.mapper.ClazzMapper;
import com.itheima.springboottilasmanager.mapper.StudentMapper;
import com.itheima.springboottilasmanager.pojo.domain.Clazz;

import com.itheima.springboottilasmanager.pojo.domain.PageResult;
import com.itheima.springboottilasmanager.pojo.vo.StudentQuery;
import com.itheima.springboottilasmanager.pojo.domain.Student;
import com.itheima.springboottilasmanager.survice.StudentService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class StudentServiceIml extends ServiceImpl<StudentMapper,Student> implements StudentService {


    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClazzMapper clazzMapper;
    @Override
    public PageResult<Student> queryAll(StudentQuery sq) {
      Page<Student> page =  Page.of(sq.getPage(), sq.getPageSize());
       LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<Student>()
               .like(sq.getName() != null, Student::getName, sq.getName());
       page(page,wrapper);
       List<Student> studentList = page.getRecords();

       if (CollectionUtil.isNotEmpty(studentList)) {
           List<Integer> SundentIds = studentList.stream().map(Student::getClazzId).toList();
           List<Clazz> clazzList = clazzMapper.selectAll();
           if (CollectionUtil.isNotEmpty(clazzList)) {
       studentList.forEach(
               student -> {
                   clazzList.stream().filter(clazz -> clazz.getId().equals(student.getClazzId()))
                           .findFirst().ifPresent(clazz -> student.setClazzName(clazz.getName()));
               }
       );
           }
       }
        return new PageResult<>(page.getTotal(),page.getRecords());
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(List<Integer> ids) {
      removeBatchByIds(ids);
    }
    @Override
    public boolean addStudent(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        return save(student);
    }
    @Override
    public Student selectById(Integer id) {
        Student student = studentMapper.selectByClassName(id);
        return student ;

    }
    @Override
    public boolean updateStudent(Student student) {
        return updateById(student);
    }
    @Override
    public Boolean addViolation(Integer id, Double score) {
      Boolean b = studentMapper.addViolation(id, score) > 0;
        return  b;
    }
}
